package view;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;

import controller.CommandManager;

import model.Perspective;

@SuppressWarnings("serial")
public class MainForm extends JFrame {

	// Attributs
	private Perspective mainPerspective;
	private JMenuBar menuBar;
	private JMenu menuFile;
	private JMenuItem menuSave;
	private JMenuItem menuLoad;
	private PictureView picViewOne;
	private Thumbnail thumbnail;
	private ControlPanel ctrlPanelOne;
	private JMenu menuEdition;
	private JMenuItem menuUndo;
	private JMenuItem menuRedo;
	
	// Accesseurs

	// /////////////
	// DEMARRAGE //
	// /////////////
	public static void main(String args[]) {
		MainForm frmMain = new MainForm();
		frmMain.setLayout(new GridLayout(2,3));
		frmMain.setSize(800, 600);
		
		frmMain.mainPerspective = new Perspective();
		//frmMain.initializeViews();
		frmMain.initializeMenu();
		frmMain.setVisible(true);

		frmMain.setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	// Methodes privees

	private void initializeMenu(){
		menuBar = new JMenuBar();
		menuFile = new JMenu("Fichier");
		menuSave = new JMenuItem("Sauvegarder");
		menuLoad = new JMenuItem("Charger");
		
		menuSave.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Save();
			}
		});
		menuSave.setAccelerator(KeyStroke.getKeyStroke(
				KeyEvent.VK_S, ActionEvent.CTRL_MASK));
		
		menuLoad.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent e) {
				Load();
			}
		}
		);
		
		menuFile.add(menuSave);
		menuFile.add(menuLoad);
		menuBar.add(menuFile);
		menuEdition = new JMenu("Edition");
		
		menuUndo = new JMenuItem("Annuler");
		menuUndo.setAccelerator(KeyStroke.getKeyStroke(
					KeyEvent.VK_Z, ActionEvent.CTRL_MASK));
		menuUndo.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Undo();
			}
		});
		
		
		menuRedo = new JMenuItem("Retablir");
		menuRedo.setAccelerator(KeyStroke.getKeyStroke(
				KeyEvent.VK_Y, ActionEvent.CTRL_MASK));
		menuRedo.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent arg0) {
				Redo();
			}
		});
		
		
		menuFile.add(menuSave);
		menuEdition.add(menuUndo);
		menuEdition.add(menuRedo);
		
		menuBar.add(menuFile);
		menuBar.add(menuEdition);
		
		setJMenuBar(menuBar);
	}
	
	private void Save(){
		try {
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("picture"));
			oos.writeObject(mainPerspective);
			oos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private void Load(){
		try {
			clearViews();
			ObjectInputStream ois = new ObjectInputStream(new FileInputStream("picture"));
			mainPerspective = (Perspective)ois.readObject();
			ois.close();

			initializeViews();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void clearViews(){
		if(picViewOne == null) return;
		remove(picViewOne);
		remove(thumbnail);
		remove(ctrlPanelOne);
	}
	
	private void initializeViews() {
		picViewOne = new PictureView(mainPerspective);
		thumbnail = new Thumbnail(mainPerspective);
		ctrlPanelOne = new ControlPanel(mainPerspective);
		
		this.mainPerspective.addObserver(picViewOne);
		this.mainPerspective.addObserver(thumbnail);
		this.mainPerspective.addObserver(ctrlPanelOne);
		
		this.add(picViewOne);
		this.add(thumbnail);
		this.add(ctrlPanelOne);
        
        mainPerspective.notifyObservers();
	}
	
	
	private void Undo() {
		CommandManager.getInstance().undo();
	}
	
	private void Redo() {
		CommandManager.getInstance().redo();
	}
}
